<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */
session_start();
session_regenerate_id();
require '../config.php';
include '../libs/Framework/System.php';
include '../libs/Framework/Lang.php';

System::$db = $GLOBALS['db'];

foreach (glob("../libs/Framework/*.php") as $lib) {
    if ($lib != '../libs/Framework/System.php') {
        require_once ($lib);
    }
}
    require '../language/' . $GLOBALS['setting']['language'] . '.php';
    if ($_GET['a'] == 'logout') {
        session_destroy();
        echo '<meta http-equiv="refresh" content="1;url=login.php">';
    }

    if (!empty($_POST['username']) && !empty($_POST['password'])) {
        require ('../libs/hash.php');
        $hash = new Hash($_POST['username'], $_POST['password']);
        $hash->hash();
        $password = $hash->password;

        $query = $GLOBALS['db']->query("SELECT id, rank FROM " . $GLOBALS['db_prefix'] .
            "users WHERE username = '" . $GLOBALS['db']->real_escape_string($_POST['username']) .
            "' AND password = '" . $password . "' AND rank >= '2'") or die($GLOBALS['db']->
            error);
        if ((int)$query->num_rows != 0) {
            $data = $query->fetch_array();

            $_SESSION["logged"] = true;
            $_SESSION['id'] = $data['id'];
            $_SESSION['rank'] = $data['rank'];
            $_SESSION['admin'] = 1;
            $log = new Log('database');
            $log->write(8, 'com_user', 'NOW()', 'User ' . $_POST['username'] .
                ' has logged into administration', $data['id']);
            $message = '<p class="msg done">'.Lang::getFront('com_user', 'login_success_1').' !</p>';

            $message .= '<meta http-equiv="refresh" content="3;url=index.php">';
        } else {
            $message = '<p class="msg error">'.Lang::getFront('com_user', 'login_bad').' !</p>';
        }
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>VisionCMS - <?php echo Lang::getBackGlobal('title_admin'); ?> - <?php echo Lang::getBackGlobal('title_login'); ?></title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="css/login-style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">


<div id="masthead">
</div>

<div id="top_nav">
</div>
<div id="right">

<fieldset>
<legend><?php echo Lang::getBackGlobal('title_login'); ?></legend>
<?php echo $message; ?>
<form action="login.php" method="POST">
<table> 
<tr><td><?php echo Lang::getFront('com_user', 'username'); ?>:</td><td><input type="text" name="username" size="20" /></td></tr>
<tr><td><?php echo Lang::getFront('com_user', 'password'); ?>:</td><td><input type="password" name="password" size="20" /></td></tr>
<tr><td><input type="submit" value="<?php echo Lang::getFrontGlobal('submit'); ?>" /></td><td></td></tr>
</table>
</form>
</fieldset>

</div>

<div id="left">


<fieldset>
<legend><?php echo Lang::getBackGlobal('login_require'); ?></legend>
<ul id="steps">
<li><?php echo Lang::getBackGlobal('login_cookies'); ?></li>
<li><?php echo Lang::getBackGlobal('login_javascript'); ?></li>
</ul>
</fieldset>

</div>


<div id="clean"></div>
<div id="footer">
<div id="left_buttons">
<p>&copy; <a rel="external" href="http://vcms.ifrit.eu/" >VisionCMS</a></p>
<p><small>Is free software released under the General Public Licence.</small></p>
</div>
<div id="right_buttons">

</div>
</div>

</div>

</body>

</html>